package com.dandelion.business.entity.vo;


import com.dandelion.business.entity.SysUser;
import com.dandelion.business.entity.vo.login.resp.Menu;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;


/**
 * 登录用户详情
 *
 * @author 77088
 * @date 2021/12/25
 */
@Data
@AllArgsConstructor
public class LoginUserDetail implements UserDetails {

    /**
     * 用户
     */
    private SysUser user;

    /**
     * 菜单
     */
    private List<Menu> menus;

    /**
     * 权限
     */
    private List<String> permissions;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> list = new HashSet<>();
        for (String permission : permissions) {
            GrantedAuthority authority = new SimpleGrantedAuthority(permission);
            list.add(authority);
        }
        return list;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
